package search

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;
import org.apache.lucene.store.FSDirectory;
import java.io.IOException;

String querystr =  "film";

StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_41);

String indexStr ="C:\\Users\\laurie\\Java\\indexes" ;
String field = "contents";

Query q = new QueryParser(Version.LUCENE_41, "contents", analyzer).parse(querystr);

// 3. search
int hitsPerPage = 100;
IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(indexStr)))
IndexSearcher searcher = new IndexSearcher(reader);
TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
searcher.search(q, collector);
ScoreDoc[] hits = collector.topDocs().scoreDocs;

// 4. display results
System.out.println("Found " + hits.length + " hits.");
for(int i=0;i<hits.length;++i) {
	int docId = hits[i].doc;
	Document d = searcher.doc(docId);
	println((i + 1) + ". " + d.get("contents") + "\t" + d.get("category"));
}

reader.close();
